package com.hyper.mongodb_monitor.ui;

import java.net.UnknownHostException;
import java.util.List;

import com.hyper.mongodb_monitor.MainApplication;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.event.Action;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.MenuBar;
import com.vaadin.ui.MenuBar.Command;
import com.vaadin.ui.MenuBar.MenuItem;
import com.vaadin.ui.Panel;
import com.vaadin.ui.Tree;
import com.vaadin.ui.Window.Notification;

@SuppressWarnings("serial")
public class MainGui extends Panel implements Property.ValueChangeListener, Action.Handler{

    private HorizontalSplitPanel horizontalSplitPanel;
    private Panel leftPanel;
    private Panel rightPanel;
    private MenuBar topMenubar;
    private Tree tree;
    
    private MongoClient client;
    private DB db;
    public List<String> databases;    
    
    
    // Actions for the context menu
    private static final Action ACTION_ADD = new Action("Add child item");
    private static final Action ACTION_DELETE = new Action("Delete");
    private static final Action ACTION_DUPLICATE = new Action("Duplicate");
    private static final Action ACTION_SHELL = new Action("Open Shell");
    private static final Action[] ACTIONS = new Action[] { ACTION_ADD, ACTION_DELETE , ACTION_DUPLICATE , ACTION_SHELL};

    public MainGui(){
        initializeSplitPanel();
        initializeLeftMenu();
        initializeRightMenu();
    }

    private void initializeSplitPanel(){
        this.setSizeFull();      
        topMenubar = new MenuBar();

        horizontalSplitPanel = new HorizontalSplitPanel();
        leftPanel = new Panel();
        rightPanel = new Panel();

        horizontalSplitPanel.setFirstComponent( leftPanel );
        horizontalSplitPanel.setSecondComponent( rightPanel );
        horizontalSplitPanel.setSizeFull();
        horizontalSplitPanel.setSplitPosition( 50 );
        
        leftPanel.setSizeFull();
        rightPanel.setSizeFull();

        MenuBar.MenuItem beverages = topMenubar.addItem( "Test", null, null );

        MenuBar.MenuItem test = beverages.addItem( "Test2", null, new Command(){

            public void menuSelected( MenuItem selectedItem ){
                MainApplication.mainWindow.showNotification( "Clicked Item..", selectedItem.getText(), Notification.TYPE_WARNING_MESSAGE, true );
            }
        } );

        
        this.addComponent( topMenubar );
        this.addComponent( new Label( "<br/><br/>", Label.CONTENT_XHTML ) );
        this.addComponent( horizontalSplitPanel );

    }

    private void initializeLeftMenu(){

    	try {
            client = new MongoClient("localhost" , 27017);
            databases = client.getDatabaseNames();

            tree = new Tree();
            
            
            // Cause valueChange immediately when the user selects
            tree.setImmediate(true);
            tree.addListener(this);
            tree.addActionHandler(this);
            
            for (String database : databases) {
                db = client.getDB(database);
                tree.addItem(database);
                for (String collection : db.getCollectionNames()) {
                	tree.addItem(collection);
                	tree.setParent(collection, database);
                }
            }
            
        } catch (UnknownHostException ex) {
        	System.out.println(ex.toString());
        }
    
        tree.setSizeFull();

        leftPanel.addComponent( tree );

    }

    private void initializeRightMenu(){
        rightPanel.addComponent( new Label( "TEST2" ) );

    }

	public Action[] getActions(Object target, Object sender) {
		System.out.println(target+"--"+sender);
		return ACTIONS;
	}

	public void handleAction(Action action, Object sender, Object target) {
		//System.out.println("handler");
		
		
		if(action == ACTION_ADD)
		{
			 tree.setChildrenAllowed(target, true);
	         tree.expandItem(target);

	         // Create new item, set parent, disallow children (= leaf node)
	            Object itemId = tree.addItem();
	            tree.setParent(itemId, target);
	            tree.setChildrenAllowed(itemId, false);

	            // Set the name for this item (we use it as item caption)
	            Item item = tree.getItem(itemId);
	            Property name = item.getItemProperty("asd");
	            name.setValue("New Item");
		}else if(action == ACTION_DELETE)
		{
			System.out.println("delete ACtion");
		}
	}
	

	public void valueChange(ValueChangeEvent event) {
		System.out.println("changed");
	}

}
